//
//  ServerRootViewController.m
//  ws-mgmt-client
//
//  Created by Cliff Duffey on 10/25/09. Mage Labs.
//

#import "ServerRootViewController.h"
#import "ConfigureServerViewController.h"
#import "ServerEventsViewController.h"

@implementation ServerRootViewController
@synthesize server, serverListIndex;
@synthesize firstTabController;
@synthesize secondTabController;
@synthesize thirdTabController;
@synthesize fourthTabController;


/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
		
	//Add some tabs to the controller...
	firstTabController = [[ConfigureServerViewController alloc] initWithNibName: @"ConfigureServerViewController" bundle: nil];
	firstTabController.tabBarItem.title = @"Config";
	firstTabController.tabBarItem.image = [UIImage imageNamed:@"19-gear.png"];
	secondTabController = [[ServerEventsViewController alloc] initWithNibName: @"ServerEventsViewController" bundle: nil];
	secondTabController.tabBarItem.title = @"System";
	secondTabController.tabBarItem.image = [UIImage imageNamed:@"69-display.png"];
	thirdTabController = [[ServerEventsViewController alloc] initWithNibName: @"ServerEventsViewController" bundle: nil];
	thirdTabController.tabBarItem.title = @"Application";
	thirdTabController.tabBarItem.image = [UIImage imageNamed:@"81-dashboard.png"];
	fourthTabController = [[ServerEventsViewController alloc] initWithNibName: @"ServerEventsViewController" bundle: nil];
	fourthTabController.tabBarItem.title = @"Security";
	fourthTabController.tabBarItem.image = [UIImage imageNamed:@"54-lock.png"];

	//Initialize the values for the ConfigServerView controller
	firstTabController.url = [server objectForKey:@"URL"];
	firstTabController.name = [server objectForKey:@"Name"];
	firstTabController.username = [server objectForKey:@"Username"];
	firstTabController.password = [server objectForKey:@"Password"];
	firstTabController.isNewServer = NO;
	firstTabController.listIndex = serverListIndex; 
	NSLog(@"%@\t%@", firstTabController.url, firstTabController.name);
	
	//Intialize the values for the ServerEventsView controller
	secondTabController.serverListIndex = serverListIndex;
	secondTabController.serverURL = [server objectForKey:@"URL"];
	NSString *username = [server valueForKey:@"Username"];
	NSString *password = [server valueForKey:@"Password"];
	NSString *serverCredentials = [[NSString alloc] initWithFormat: @"%@:%@", username, password];
	NSString *encodedCredentials = [NSString base64StringFromData: [serverCredentials dataUsingEncoding:NSASCIIStringEncoding] length: [serverCredentials length]];	
	secondTabController.credentials = [NSString stringWithFormat: @"Basic %@", encodedCredentials]; 
	secondTabController.requestType = systemLog;
	
	//Intialize the values for the AppEventsView controller
	thirdTabController.serverListIndex = serverListIndex;
	thirdTabController.serverURL = [server objectForKey:@"URL"];
	thirdTabController.credentials = [NSString stringWithFormat: @"Basic %@", encodedCredentials]; 
	thirdTabController.requestType = appLog;
	 
	//Intialize the values for the SecurityEventsView controller
	fourthTabController.serverListIndex = serverListIndex;
	fourthTabController.serverURL = [server objectForKey:@"URL"];
	fourthTabController.credentials = [NSString stringWithFormat: @"Basic %@", encodedCredentials]; 
	fourthTabController.requestType = securityLog;
	
	self.viewControllers = [NSArray arrayWithObjects:firstTabController, secondTabController, thirdTabController, fourthTabController, nil];

	[firstTabController release];
	[secondTabController release];
	[thirdTabController release];
	[fourthTabController release];
		
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
